<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.img.php');
$msgPag = reqVar('msgpag');
$sql_sel = "SELECT d.cod_tabella, d.descr, d.lista_campi, v.* " .
		" FROM rgl_tabelle_val v " .
		" INNER JOIN rgl_tabelle d ON d.id_tabella = v.id_tabella " .
		" WHERE v.id_tabella = ".intval(reqVar('id')).
		" AND LOWER(chiave) = '".reqVarAS('key')."'";
$objDB->query($sql_sel,'selRecDom');
$acRecDom = $objDB->fetch('selRecDom');
if (!$acRecDom) {
	header("location: tabelleList.php?msgpag=".urlencode("tabelleView - manca id/chiave").
		"&".$sXSNQS);
	exit();
}
if (reqVar('azione') == 'elimina') {
	$msgPag = elimina();
} 
$aLabCampi = explode(",",$acRecDom['lista_campi']);

testaHtml('Gestione applicazione',$msgPag,'gest');
echo getHtmlForm();
echo getHtmlHidden('azione','view');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('key');
?>
<script type="text/javascript">
function confElimina() {
	if (confirm("Confermi eliminazione elemento ?")) {
		document.frm1.azione.value = 'elimina';
		document.frm1.submit();
	}
}
function goModifica() {
	document.frm1.action = 'tabelleValMod.php';
	document.frm1.azione.value = '';
	document.frm1.submit();
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
<?php 
if (true) {
?>
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:confElimina();" >elimina</a>
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:goModifica();" >modifica</a>
<? 
}
echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" width='20%' align='left'>Tabella</td>
      <td class="txt1" width='80%' align='left' colspan='3'>
			<span style='font-family:lucida console,courier;'><?=$acRecDom['cod_tabella']?></span>
			<?=' - '.$acRecDom['descr']?>
		</td>
   </tr>
<?
foreach($aLabCampi as $kX => $labX) {
	if ($kX == 0) {
		$ncX = 'chiave';
?>
   <tr>
      <td class="lab1"><?=$labX?> (chiave)</td>
		<td class="txt1" width='60%'>
			<?=getHtmlViewNote($acRecDom['chiave']);?>
		</td>
      <td class="lab1" width='10%'>Scaduto</td>
		<td class="txt1" width='10%'>
			<?=($acRecDom['scaduto']=='S')?"Si'":"No";?>
		</td>
   </tr>
<?
		continue;
	}
	if ($kX > 12) {break;}
	$ncX = "c".str_pad($kX,2,'0',STR_PAD_LEFT);
?>
   <tr>
      <td class="lab1"><?=$labX?></td>
		<td class="txt1" colspan='3'>
			<?=getHtmlViewNote($acRecDom[$ncX]);?>
		</td>
   </tr>
<?
}
?>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function elimina() {
	global $objDB;
	$sSqlIns = "DELETE FROM rgl_tabelle_val ";
	$sSqlIns .= " WHERE id_tabella = ".intval(reqvar('id'));
	$sSqlIns .= " AND LOWER(chiave) = '".addslashes(strtolower(reqVar('key')))."'";
	$objDB->exec($sSqlIns);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: tabelleView.php?id=".intval(reqvar('id')).
		"&msgpag=".urlencode('chiave '.reqVar('key').' eliminata')."&xsn=".XSN);
	exit();
}
